package com.shanzmoo.admin.web.product;


import com.shanzmoo.base.bean.SHttpResponse;
import com.shanzmoo.base.bean.SResponse;
import com.shanzmoo.core.cache.CurrentUserCache;
import com.shanzmoo.db.product.entity.ProductCategoryEntity;
import com.shanzmoo.db.product.entity.dto.CategoryUpdateDto;
import com.shanzmoo.db.product.entity.vo.CategoryVo;
import com.shanzmoo.db.product.service.IProductCategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 商品分类 前端控制器
 * </p>
 *
 * @author Xueqz
 * @since 2020-12-07
 */
@Api(tags = "商品-商品分类")
@RestController
@RequestMapping("/api/product/category")
public class ProductCategoryController {

    @Autowired
    private IProductCategoryService productCategoryService;

    @ApiOperation(value = "分类列表树")
    @GetMapping("/getCategoryTree")
    public SHttpResponse<List<CategoryVo>> getCategoryTree() {
        SResponse<List<CategoryVo>> re = productCategoryService.getAllTree();
        return SHttpResponse.transform(re);
    }

    @ApiOperation(value = "删除商品品牌")
    @GetMapping("/delCatById")
    public SHttpResponse<Boolean> delBrandById(@RequestParam(value = "catId") Integer catId) {
        ProductCategoryEntity productCategoryEntity = new ProductCategoryEntity();
        productCategoryEntity.setId(catId);
        productCategoryEntity.setUpdateUser(CurrentUserCache.getUserId());
        productCategoryEntity.setUpdateTime(LocalDateTime.now());
        productCategoryEntity.setDeleted(true);
        productCategoryService.updateById(productCategoryEntity);

        return SHttpResponse.ok(Boolean.TRUE);
    }

    @ApiOperation(value = "添加商品分类")
    @GetMapping("/addCat")
    public SHttpResponse<Boolean> addCat(ProductCategoryEntity categoryEntity) {

        categoryEntity.setCreateUser(CurrentUserCache.getUserId());
        productCategoryService.save(categoryEntity);

        return SHttpResponse.ok(Boolean.TRUE);
    }


    @ApiOperation(value = "编辑商品分类")
    @PostMapping("/editCat")
    public SHttpResponse<Boolean> editCat(@RequestBody CategoryUpdateDto categoryUpdateDto) {

        ProductCategoryEntity categoryEntity = CategoryUpdateDto.converted(categoryUpdateDto);
        categoryEntity.setUpdateUser(CurrentUserCache.getUserId());

        return SHttpResponse.ok(Boolean.TRUE);
    }

}
